/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

/**
 *
 * @author Ricardo Orellana
 */

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Recolector;
import muhnes.sizoo.HibernateUtilZoologia;

public class RecolectorDaoImpl implements RecolectorDao{

    Session sesion;
    
    public RecolectorDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Recolector recolector ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( recolector );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Recolector > recolectores ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( recolectores );
        transaccion.commit();
    }

    @Override
    public void actualizar( Recolector recolector ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( recolector );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Recolector > recolectores ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( recolectores );
        transaccion.commit();
    }

    @Override
    public void eliminar( Recolector recolector ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( recolector );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Recolector > recolectores ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( recolectores );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Recolector recolector = buscarPorId( id );
        sesion.delete( recolector );
        transaccion.commit();
    }

    @Override
    public Recolector buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Recolector where CodigoRecolector = " + id);
        Recolector recolector = (Recolector)consulta.uniqueResult();
        transaccion.commit();
        return recolector;
    }

    @Override
    public List< Recolector > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Recolector");
        List< Recolector > recolectores = consulta.list();
        transaccion.commit();
        return recolectores;
    }
    
}
